<?php

namespace app\home\controller;

use think\facade\Cookie;
use think\Db;

class Classes extends BaseController
{
    protected $readonly = ['name', 'password'];
    
    public function index($id = 'dy')
    {
        # code...
        $index = '';
        $menuattr = array(
            'submenu' => array(
                array(
                    'index' => 1,
                    'icon' => 'el-icon-location',
                    'title' => '班级',
                    'item' => array(
                        array(
                            'index' => '1-1',
                            'content' => '最新动态'
                        ),
                        array(
                            'index' => '1-2',
                            'content' => '我的同学'
                        )
                    )
                )
            ),
            'item' => array(
                array(
                    'index' => '3-1',
                    'icon' => 'el-icon-menu',
                    'content' => '相册'
                ),
                array(
                    'index' => '4-1',
                    'icon' => 'el-icon-menu',
                    'content' => 'Vlog'
                )
            )
        );
        if ($id == 'dy') {
            $index = '1-1';
            $this->assign('data', $this->dynamic());
            $this->assign('total', $this->dynamic()['last_page'] * 10);
        } elseif ($id == 'mate') {
            $index = '1-2';
            $this->assign('matedata', $this->mate());
        }
        $this->assign('list', $menuattr);
        $this->assign('menu', $index);
        return $this->fetch();
    }
    public function user()
    {
        // $list = model('classes')->withJoin(['user' => function($query){
        //     $query->withField('username,sex,avatar,title');
        // }])->paginate(5)->toArray();
        // // array_push($list,)
        // // print_r($list);
        // // foreach ($list['data'] as $key => $value) {
        // //     // print_r($value['user']);
        // // }
        // $article = model('Classes')->with('comment,comment.reply')->find(input('id'))->toArray();


        // // $article = model('classes')->withCount('comment')->withJoin(['user' => function($query){
        // //     $query->withField('username,sex,avatar,title');
        // // }])->all();
        // $article = model('Classes')->with(['user','comment'])->select();

        // // print_r(model('Classes')->all()->comment);
        // return json($article);
        $new_list = array();
        // $list = model('classes')->withJoin(['user' => function ($query) {
        //     $query->withField('username,sex,avatar,title');
        // }])->paginate(5)->toArray();
        // foreach ($list['data'] as $key => $value) {
        //     // print_r($key);
        //     $com = model('comment')->where('pid', $value['id'])->select()->toArray();
        //     if (!empty($com)) {
        //         $list['data'][$key]['comments'] = count($com);
        //     }
        // }
        // $article = model('Classes')->with(['user','comment','comment.user','comment.reply'])->find(1)->toArray();
        // return $id;
        // $article = model('Comment')->where('pid',1)->with(['user','reply','reply.member'])->select()->toArray();
        // foreach ($article['comment'] as $key => $value) {
        //     print_r($value);
        // }
        // print_r($article);
        // $data = [
        //     'accound' => 123,
        //     'usercard' => 456,
        //     'password' => 'zyq'
        // ];
        // model('user')->where($data)->find();

        // return Db::getLastSql();
        //   print_r($article['comment']);
        // print_r($new_list[0]);
        $article = model('reply')->with('member')->get(1);
        $comment = model('Comment')->where('pid',1)->with(['user','reply','reply.member'])->paginate(5);
        return json($comment);
    }
    public function mate()
    {
        # code...
        $result = model('user')->select();
        $result->hidden(['password']);


        // return ;
    }
    public function dynamic()
    {
        # code...
        $list = model('classes')->order('id')->withJoin(['user' => function ($query) {
            $query->withField('username,sex,avatar,title');
        }])->paginate(5)->toArray();
        foreach ($list['data'] as $key => $value) {
            // print_r($key);
            $com = model('comment')->where('pid', $value['id'])->select()->toArray();
            if (!empty($com)) {
                $list['data'][$key]['comments'] = count($com);
            }
        }
        return $list;
    }
    public function getDynamicData()
    {
        # code...
    }
    public function dydetail()
    {
        # code...
        $article = model('Classes')->with('user,comment')->find(input('id'))->toArray();
        // return $id;
        $article['comments'] = count($article['comment']);
        $comment = model('Comment')->where('pid',input('id'))->with(['user','reply','reply.member'])->paginate(5);
        $this->assign('pid',input('id'));
        $this->assign('data', $article);
        $this->assign('list',$comment);
        return view();
    }
    public function post()
    {
        # code...

        if (request()->isAjax()) {
            $data = [
                'title' => input('post.title'),
                'content' => input('post.content'),
                'userid' => Cookie::get('userid'),
                'time' => date('Y-m-d H:i:s')
            ];

            $result = model('Classes')->post($data);
            if ($result == 1) {
                // return json(array('code' => 1, 'msg' => '发表成功'));
                $this->success('投稿成功！', '/cls/dy');
            } else {
                $this->error($result);
            }
        }

        return view();
    }
}
